/**
 * file
 */
package com.tdkj.action.front.member.friendsManagement;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.hibernate.Criteria;
import org.hibernate.Query;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;

import com.opensymphony.xwork2.ActionContext;
import com.tdkj.action.BaseAction;
import com.tdkj.model.user.BeanFriend;
import com.tdkj.model.user.BeanFriendCategory;
import com.tdkj.model.user.BeanMessage;
import com.tdkj.model.user.BeanUser;
import com.tdkj.service.user.IBeanFriendCategoryService;
import com.tdkj.service.user.IBeanMessageService;
import com.tdkj.service.user.IBeanUserService;

/**

 * FileName:     MyFriendsAction.java

 * @Description: TODO我的好友

 * All rights Reserved, Designed By ZTE-ITS

 * Copyright:    Copyright(C) 2010-2011

 * Company       天成科技有限公司

 * @author:    李兴建

 * @version    V1.0 

 * Createdate:         2012-5-27 下午12:22:34

 *

 * Modification  History:

 * Date         Author        Version        Discription

 * -----------------------------------------------------------------------------------

 * 2012-5-27       李兴建          1.0             1.0

 * Why & What is modified: <修改原因描述>

 */
public class MyFriendsAction extends BaseAction {
	private Long fid;//好友id
	private Long uid;//用户id
	private Long cid;//分类id
	private Long mid;//信息id
	private String searchname;
	private String categoryname;
	private String mobilephone;
	private String messagecontent;
	private List<BeanFriendCategory> categorys=new ArrayList<BeanFriendCategory>();
	private BeanFriendCategory category;
	private BeanFriend friend;
	private IBeanUserService userService;
	private IBeanFriendCategoryService categoryService;
	private IBeanMessageService mesageService;
	public Long getFid() {
		return fid;
	}
	public void setFid(Long fid) {
		this.fid = fid;
	}
	public Long getCid() {
		return cid;
	}
	public void setCid(Long cid) {
		this.cid = cid;
	}
	public Long getUid() {
		return uid;
	}
	public void setUid(Long uid) {
		this.uid = uid;
	}
	public Long getMid() {
		return mid;
	}
	public void setMid(Long mid) {
		this.mid = mid;
	}
	public String getSearchname() {
		return searchname;
	}
	public String getMobilephone() {
		return mobilephone;
	}
	public void setMobilephone(String mobilephone) {
		this.mobilephone = mobilephone;
	}
	public void setSearchname(String searchname) {
		this.searchname = searchname;
	}
	public String getMessagecontent() {
		return messagecontent;
	}
	public void setMessagecontent(String messagecontent) {
		this.messagecontent = messagecontent;
	}
	public String getCategoryname() {
		return categoryname;
	}
	public void setCategoryname(String categoryname) {
		this.categoryname = categoryname;
	}
	public BeanFriendCategory getCategory() {
		return category;
	}
	public void setCategory(BeanFriendCategory category) {
		this.category = category;
	}
	public BeanFriend getFriend() {
		return friend;
	}
	public void setFriend(BeanFriend friend) {
		this.friend = friend;
	}
	
	public List<BeanFriendCategory> getCategorys() {
		return categorys;
	}
	public void setCategorys(List<BeanFriendCategory> categorys) {
		this.categorys = categorys;
	}
	public IBeanFriendCategoryService getCategoryService() {
		return categoryService;
	}
	@Resource
	public void setCategoryService(IBeanFriendCategoryService categoryService) {
		this.categoryService = categoryService;
	}
	public IBeanUserService getUserService() {
		return userService;
	}
	@Resource
	public void setUserService(IBeanUserService userService) {
		this.userService = userService;
	}
	public IBeanMessageService getMesageService() {
		return mesageService;
	}
	@Resource
	public void setMesageService(IBeanMessageService mesageService) {
		this.mesageService = mesageService;
	}
	public String findAll(){
		initFriend();
		BeanUser u=this.getCurrentUser();
		u = categoryService.findById(u.getId(), BeanUser.class);
		categoryService.getBaseDao().getSession().refresh(u);
		categorys= u.getFriendCategorys();
		String hql = "select bean from " + BeanFriend.class.getName() + " as bean ,"
		+" "+BeanUser.class.getName() +" as user, "
		+" "+BeanFriendCategory.class.getName() + " as category"
		+" where bean.category.id=category.id and category.user.id=user.id and user.id='"+u.getId()+"' ";
		if(this.getCid()!=null){
			hql = hql + " and category.id="+this.getCid();
		}
		if(StringUtils.isNotEmpty(this.getSearchname())){
			hql = hql + " and bean.fname like '%"+searchname+"%' ";
		}
		if(StringUtils.isNotEmpty(this.getMobilephone())){
			hql = hql + " and user.personal.mobilephone like '%"+mobilephone+"%' ";
		}
		
		hql = hql + " order by bean.createtime";
		Query query = this.createQuery(hql);
		this.queryByPage(query, "myFriendsAction!findAll.action", 10);
		return "index";
	}
	public String findMsgHistory(){
		BeanUser ru=userService.findById(this.getFid(),BeanUser.class);
		BeanUser cu=this.getCurrentUser();
		Long [] s={ru.getId(),cu.getId()};
		Criteria criteria = this.createCriteria(BeanMessage.class);
		criteria.add(Restrictions.in("sender.id",s));
		criteria.add(Restrictions.in("reciver.id",s));
		criteria.addOrder(Order.desc("createtime"));// 对状态进行排序
		this.queryByPage(criteria, "myFriendsAction!findMsgHistory.action", 10);
		return "msg_history";
	}
	public String addCategory(){
		BeanUser u=this.getCurrentUser();
		BeanFriendCategory c=new BeanFriendCategory();
		c.setName(this.getCategoryname());
		c.setCreatetime(new Timestamp(System.currentTimeMillis()));
		c.setUser(u);
		categoryService.save(c);
		return findAll();
	}
	public String editCategory(){
		BeanFriendCategory c=categoryService.findById(this.getCid(),BeanFriendCategory.class);
		c.setName(this.getCategoryname());
		categoryService.save(c);
		return findAll();
	}
	public String deleteFriend(){
		this.getBaseService().delete(this.getFid(), BeanFriend.class);
		return findAll();
	}
	public String deleteCategory(){
		categoryService.delete(this.getCid(), BeanFriendCategory.class);
		this.setCid(null);
		return findAll();
	}
	public String deleteMsg(){
		fid=this.getFid();
		uid=this.getUid();
		if(fid==this.getCurrentUser().getId()){
			fid=uid;
		}
		mesageService.delete(this.getMid(), BeanMessage.class);
		return findMsgHistory();
	}
	public String updateCategory(){
		BeanFriend f=this.getBaseService().findById(this.getFid(), BeanFriend.class);
		BeanFriendCategory c=categoryService.findById(this.getCid(), BeanFriendCategory.class);
		f.setCategory(c);
		this.getBaseService().saveOrUpdate(f, BeanFriend.class);
	//	cid=c.getId();
		return findAll();
	}
	//发送短消息
	public String addMessage(){
		BeanUser ru=userService.findById(this.getFid(),BeanUser.class);
		BeanMessage bean=new BeanMessage();
		bean.setContent(this.getMessagecontent());
		bean.setCreatetime(new Timestamp(System.currentTimeMillis()));
		bean.setMsgtype(1);
		bean.setSender(this.getCurrentUser());
		bean.setReciver(ru);
		mesageService.save(bean);
		return findAll();
	}
	//添加好友
	public String addFriend(){
		BeanUser cu=this.getCurrentUser();//当前用户的id
		BeanUser fu=userService.findById(this.getUid(),BeanUser.class);//被请求用户的id
		BeanFriendCategory ccu=categoryService.findDefaultCategory(cu.getId());
		BeanFriendCategory cfu=categoryService.findDefaultCategory(fu.getId());
		BeanFriend f1=new BeanFriend();
		f1.setCreatetime(new Timestamp(System.currentTimeMillis()));
		f1.setFid(fu.getId());
		f1.setFname(fu.getUsername());
		f1.setCategory(cfu);
		this.getBaseService().save(f1);
		BeanFriend f2=new BeanFriend();
		f2.setCreatetime(new Timestamp(System.currentTimeMillis()));
		f2.setFid(cu.getId());
		f2.setFname(cu.getUsername());
		f2.setCategory(ccu);
		this.getBaseService().save(f2);
		return "";
	}
	//初初始好友
	private void initFriend() {
		// TODO Auto-generated method stub
		BeanUser u=this.getCurrentUser();
		u = categoryService.findById(u.getId(), BeanUser.class);
		BeanFriendCategory v=null;
		List<BeanFriendCategory> cs=u.getFriendCategorys();
		if(cs.size()==0){
			v=new BeanFriendCategory();
			v.setUser(u);
			v.setName("默认");
			categoryService.save(v);
		}else{
			for(int i=0;i<cs.size();i++){
				if(cs.get(i).getName().equals("默认")){
					v=cs.get(i);
				}
			}
		}
		List<BeanUser> l=this.getBaseService().findAll(BeanUser.class);
		List<BeanFriend> ld=new ArrayList<BeanFriend>();
		for(int i=0;i<l.size();i++){
			BeanUser b=l.get(i);
			BeanFriend f=new BeanFriend();
			f.setCreatetime(new Timestamp(System.currentTimeMillis()));
			f.setFid(b.getId());
			f.setFname(b.getUsername());
			f.setCategory(v);
			this.getBaseService().save(f);
		}
		categoryService.saveOrUpdate(v, BeanFriendCategory.class);
		this.getBaseService().saveOrUpdate(u, BeanUser.class);
	}
	
}
